package com.tcc.fastline.Fragments;

import java.util.List;

import com.tcc.fastline.SessionManager;
import com.tcc.fastline.BLL.BLLFactory;
import com.tcc.fastline.BLL.UsuarioBS;
import com.tcc.fastline.BLL.interfaces.IUsuarioBS;
import com.tcc.fastline.Domain.Usuario;
import com.tcc.fastline.Exceptions.ValidationServerException;
import com.tcc.fastline.Helpers.BuilderHelper;
import com.tcc.fastline.Helpers.GridViewUsuarioAdapter;
import com.tcc.smartqueue.R;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ConsultaUsuarioFragment extends ConteudoFragment {

	SessionManager session;
	private View _view;
	private Context context;

	GridView listViewUsuarios = null;
	IUsuarioBS userBS = null;

	@Override
	public ConteudoFragment newInstance() {
		ConsultaUsuarioFragment newFragment = new ConsultaUsuarioFragment();

		Bundle argumentsBundle = new Bundle();

		newFragment.setArguments(argumentsBundle);

		return newFragment;
	}

	@Override
	public ConteudoFragment newInstance(Bundle argumentsBundle) {
		return newInstance();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.consulta_usuario_fragment,
				null);
		context = rootView.getContext();
		_view = rootView.findViewById(R.id.consulta_usuario);

		userBS = BLLFactory.getUsuarioBS(context);

		InitializeFields(rootView);
		PreencherListUsuarios();

		return rootView;
	}

	private void PreencherListUsuarios() {
	}

	private void InitializeFields(View rootView) {
		List<Usuario> listaUsuarios;
		try {
			listViewUsuarios = (GridView) rootView
					.findViewById(R.id.list_usuarios);

			listaUsuarios = userBS.BuscarUsuarios();

			GridViewUsuarioAdapter adapter = new GridViewUsuarioAdapter(
					getActivity(), listaUsuarios);
			listViewUsuarios.setAdapter(adapter);
			listViewUsuarios.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			listViewUsuarios.setBackgroundColor(Color.WHITE);
			listViewUsuarios.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> adp, View v,
						int position, long arg3) {

					Usuario user = (Usuario) adp.getItemAtPosition(position);

					Toast.makeText(context, user.getNome(), Toast.LENGTH_SHORT)
							.show();
				}
			});

			adapter.notifyDataSetChanged();
		}  catch (ValidationServerException e) {
			ExibirValidationServerException(e.getMessage());
		} catch (Exception e) {
			ExibirValidationServerException(e.getMessage());
		}
	}
	
	private void ExibirValidationServerException(String mensagem) {
		BuilderHelper.OK("Erro", mensagem, context,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
	}
}
